package com.css.fxfzmh.base.upload;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 模板下载
 */
@RestController
@RequestMapping("/base/product")
@Slf4j
public class ProductController {

    @GetMapping(value = "/download", produces = "text/html;charset=UTF-8")
    public void downloadExcel(@RequestParam String fileName, HttpServletResponse response) {
        log.info("入参: 模板名全称", fileName);
        try {
            //请求头的模板名为中文
            String file_name = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;fileName=" + file_name);
            response.setContentType("application/x-download");
            //获取文件的路径
            String path = getClass().getResource("/excel/" + fileName).getPath();
            //路径名转换中文
            String filePath = URLDecoder.decode(path, "utf-8");
            FileInputStream inputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[65536];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            log.info(fileName + ": 模板下载完成");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(fileName + ":" + e);
        }
    }

    @PostMapping(value = "/downloadExcel", produces = "text/html;charset=UTF-8")
    public void downloadExcel1(@RequestBody Map param, HttpServletResponse response) {
        //log.info("入参: 模板名全称", fileName);
        String fileName = (String) param.get("fileName");
        try {
            //请求头的模板名为中文
            String file_name = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;fileName=" + file_name);
//            response.setContentType("application/x-download");
            //获取文件的路径
            String path = getClass().getResource("/excel/" + fileName).getPath();
            //路径名转换中文
            String filePath = URLDecoder.decode(path, "utf-8");
            FileInputStream inputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[65536];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            log.info(fileName + ": 模板下载完成");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(fileName + ":" + e);
        }
    }
}
